import {query, getFunds, getConfig} from '../../services/secDome/index'
export default {
  namespace: 'secDome',
  state: {
    config: {},
    pageParam: {
      pageNo: 1,
      pageSize: 10,
    },
    param: {
      projectType: 0,
      status: ''
    },
    filterParam: {},
    loading: false,
    filterBoxShowFlag: false,
    list: [], //数据列表
    fundList:[], //创建人
    total: 0
  },

  subscriptions: {
    setup({dispatch, history}, done) {
      const unlisten = history.listen(function (location) {
        if (location.pathname === '/secDome') {
          dispatch({type: 'getConfig'});
          dispatch({
            type: 'query',
            payload: {
              pageParam: {
                pageNo: 1,
                pageSize: 10,
              },
              param: {
                projectType: 0,
                status: ''
              },
            }
          });
          dispatch({
            type: 'getFunds'
          });
        }
      });
      return unlisten;
    }
  },
  effects: {
    *getConfig({payload}, {call, put}) {//, filter:payload.filter, sort:payload.sort
      const data = yield call(getConfig);
      if (data.code == 200) {
        yield put({
          type: 'updateConfig',
          payload: {
            config:data.data
          }
        })
      }
    },
    *query({payload}, {call, put}) {//, filter:payload.filter, sort:payload.sort
      yield put({type: 'showLoading'});
      const data = yield call(query, {...payload});
      if (data.code == 200) {
        debugger;
        yield put({
          type: 'querySuccess',
          payload: {
            ...payload,
            filterBoxShowFlag: false,
            list: data.data,
            total: data.totalSize
          }
        })
      } else {
        openNotice('error', '操作失败', data.message);
        yield put({
          type: 'querySuccess',
          payload: {
            ...payload,
            list: [],
            total: 0
          }
        })
      }
    },
    *getFunds({payload}, {call, put}){
      yield put({type: 'showLoading'});
      const data = yield call(getFunds);
      if (data.code == 200) {
        yield put({
          type: 'querySuccess',
          payload: {
            fundList: data.data.length > 0 ? data.data : [],
          }
        })
      } else {
        openNotice('error', '操作失败', data.message);
        yield put({
          type: 'querySuccess',
          payload: {
            fundList: [],
          }
        })
      }
    },
  },
  reducers: {
    updateConfig(state, action) {
      return {...state, ...action.payload}
    },
    updateState(state, action) {
      return {...state, ...action.payload}
    },
    showLoading(state) {
      return {...state, loading: true}
    },
    filterBoxShowFlagChange(state, action) {
      return {...state, ...action.payload}
    },
    querySuccess(state, action){
      return {...state, ...action.payload, loading: false}
    },

    setFilterParam(state, action){
      var tmp = state.filterParam;
      tmp[action.payload.name] = action.payload.value;
      return {...state, filterParam: tmp}
    }
  }
}
